package com.aaa.market.controller;

import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.market.entity.CouponHistory;
import com.aaa.market.entity.TbCoupon;
import com.aaa.market.service.CouponHistoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 优惠券使用、领取历史表(CouponHistory)表控制层
 *
 * @author makejava
 * @since 2020-11-24 21:45:33
 */
@RestController
@RequestMapping("couponHistory")
public class CouponHistoryController {
    /**
     * 服务对象
     */
    @Resource
    private CouponHistoryService couponHistoryService;

    /**
     * 前台会员查询可用优惠券
     *
     * @param map 主键
     * @return 单条数据
     */
    @PostMapping ("queryUseCoupon")
    public Result queryUseCoupon(@RequestBody Map map) {
        return couponHistoryService.queryUseCoupon(map);
    }

    /**
     * 修改优惠券历史信息
     * @param couponHistory
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody CouponHistory couponHistory){
        //当ID空为时抛出自定义异常
        if (couponHistory.getId()==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.couponHistoryService.update(couponHistory));
    }
}